package org.oc.qaq.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.oc.qaq.annotation.Autofill;
import org.oc.qaq.entity.Role;
import org.oc.qaq.entity.Tag;
import org.oc.qaq.entity.UserRole;
import org.oc.qaq.enumeration.OperationType;

import java.util.List;

@Mapper
public interface RoleMapper {

    @Select("select id from role where name=#{name}")
    Long findRoleIdByName(String name);

    @Select("select * from role")
    List<Role> findAllRoles();

    List<String> findTagsByRoleId(Long id);

    @Delete("delete from user_role where user_id=#{id}")
    void deleteByUserId(String id);

    void batchInsertUserRole(List<UserRole> newUserRoles);

    @Autofill(OperationType.INSERT)
    void insert(Role role);

    @Autofill(OperationType.UPDATE)
    void update(Role role);

    @Select("SELECT * FROM user_role WHERE user_id=#{id}")
    List<UserRole> findRoles(Long id);

    @Delete("DELETE FROM role WHERE id=#{id}")
    void delete(Long id);

    @Select("SELECT * FROM role WHERE id IN (SELECT role_id FROM user_role WHERE user_id=#{userId})")
    List<Long> getRoleIdsByUserId(String userId);

    void insertRoleTags(Long id, List<Long> tagIds);

    void deleteRoleTags(Long id);
}
